// page/pack-mine/pages/mine-details/mine-details.js
var httpUtils = require('../../../../utils/https-utils.js')
var toolUtils = require('../../../../utils/tool-utils.js')
var topPage
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    sexs: [{ name: '男', id: 1 }, { name: '女', id: 2 }],
    multiIndex: [0, 0],
    mydata: [],
    ages: [],
    allow_age: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    topPage = getCurrentPages()[getCurrentPages().length - 2]
    var date = new Date()
    var day = date.getDate()
    var month = date.getMonth() + 1
    var year = date.getFullYear()
    that.setData({
      userInfo: topPage.data.userInfo,
      nowDate: year + '-' + month + '-' + day,
      age: toolUtils.ages(topPage.data.userInfo.user_birth||'')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindModifylogo: function (e) {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        httpUtils.uploadImgs(tempFilePaths, function (res) {
          var parm = {}
          parm.user_avatar = res[0].key
          modify(parm, 0)
        })
      }
    })
  },
  //修改昵称
  bindModifyName:function(e){
    toolUtils.pageTo('/page/pack-mine/pages/modify-name/modify-name', 1)
  },
  //修改性别
  bindSexChange: function (e) {
    console.log(e)
    that.setData({
      sexn: that.data.sexs[e.detail.value]
    })
    var param = {}
    param.user_gender = that.data.sexs[e.detail.value].id
    modify(param, 1)
  },
  //修改年龄
  bindModifyAge: function (e) {
    console.log(e)
    var birth = e.detail.value
    that.setData({
      age: toolUtils.ages(birth)
    })
    var param = {}
    param.user_birth = birth
    modify(param, 2)
  },
  //获取手机
  getPhoneNumber: function (e) {
    var iv = e.detail.iv
    var encryptedData = e.detail.encryptedData
    if (!iv && !encryptedData) {
      toolUtils.showFailToast("授权失败")
      return
    }
    getApp().getToken(function (token) {
      var params = {}
      params.token = token
      params.iv = iv
      params.encryptedData = encryptedData
      httpUtils.decrypt(params, function (res) {
        console.log(res)
        var param = {}
        param.user_phone = res.phoneNumber
        modify(param, 3)
      }, function (e) {

      })
    })
  }
})
//修改个人信息
function modify(param, flag) {
  httpUtils.updateUserInfo(param, function (res) {
    that.setData({
      userInfo: res
    })
    topPage.setData({
      userInfo: res
    })
    if(flag == 3){
      getApp().data.userPhone = res.user_phone
    }
  }, function (e) {
    toolUtils.showFailToast("修改个人信息失败")
  })
}
